package com.ruoyi.mateRequistion.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateProcurement.domain.MateProcurementModel;
import com.ruoyi.mateProcurement.mapper.MateProcurementMapper;
import com.ruoyi.mateRequistion.domain.KeepSample;
import com.ruoyi.mateRequistion.domain.KeepSampleModel;
import com.ruoyi.mateRequistion.domain.ManageInput;
import com.ruoyi.mateRequistion.domain.ManageInputModel;
import com.ruoyi.mateRequistion.domain.ManageSign;
import com.ruoyi.mateRequistion.domain.ManageSignModel;
import com.ruoyi.mateRequistion.domain.MateRequistion;
import com.ruoyi.mateRequistion.domain.MateRequistionModel;
import com.ruoyi.mateRequistion.domain.ProductRetention;
import com.ruoyi.mateRequistion.domain.ProductRetentionModel;
import com.ruoyi.mateRequistion.domain.SampleHandle;
import com.ruoyi.mateRequistion.domain.SampleHandleModel;
import com.ruoyi.mateRequistion.domain.SampleRequistion;
import com.ruoyi.mateRequistion.domain.SampleRequistionModel;
import com.ruoyi.mateRequistion.mapper.MateRequistionMapper;
import com.ruoyi.mateRequistion.service.MateRequistionService;
import com.ruoyi.mateproduction.domain.UnqualifiedReviewModel;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateRequistionServiceImpl implements MateRequistionService {

    @Autowired
    MateRequistionMapper mateRequistionMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MateProcurementMapper mateProcurementMapper;

    @Override
    public int addMateRequistion(MateRequistion mateRequistion) {
        int num = 0;
        if (mateRequistion.getRequistionModels() != null) {
            try {
                num = mateRequistionMapper.addMateRequistion(mateRequistion);
                for (MateRequistionModel mateRequistionModel : mateRequistion.getRequistionModels()) {
                    mateRequistionModel.setDeptId(SecurityUtils.getDeptId());
                    mateRequistionModel.setRequistionId(mateRequistion.getRequistionId());
                    mateRequistionMapper.addMateRequistionModel(mateRequistionModel);
                }
            } catch (Exception e) {
                mateRequistionMapper.removeMateRequistion(mateRequistion.getRequistionId());
                mateRequistionMapper.removeMateRequistionModel(mateRequistion.getRequistionId());
                Map<String, Object> resMap = new HashMap<>();
                resMap.put("orderId", mateRequistion.getRequistionId());
                resMap.put("orderType", "5");
                mtAuditMapper.deleteMtAuditById(resMap);
            }
        }

        if (num > 0) {
            return num;
        } else {
            return 0;
        }

    }

    @Override
    public List<MateRequistion> requistionList(MateRequistion mateRequistion) {
        return mateRequistionMapper.requistionList(mateRequistion);
    }

    @Override
    public MateRequistion selectMateRequistion(Long requistionId) {
        return mateRequistionMapper.selectMateRequistion(requistionId);
    }

    @Override
    public void removeMateRequistion(Long requistionId) {
        mateRequistionMapper.removeMateRequistion(requistionId);
        mateRequistionMapper.removeMateRequistionModel(requistionId);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("orderId", requistionId);
        resMap.put("orderType", "5");
        mtAuditMapper.deleteMtAuditById(resMap);
    }


    @Override
    public List<MateRequistion> AuditRequistion(Long deptId) {
        return mateRequistionMapper.AuditRequistion(deptId);
    }

    @Override
    public int updateMateRequistion(MateRequistion mateRequistion) {
        int num = 0;
        if (mateRequistion.getRequistionModels() != null) {
            mateRequistionMapper.removeMateRequistionModel(mateRequistion.getRequistionId());
            for (MateRequistionModel mateRequistionModel : mateRequistion.getRequistionModels()) {
                mateRequistionModel.setDeptId(SecurityUtils.getDeptId());
                mateRequistionModel.setRequistionId(mateRequistion.getRequistionId());
                mateRequistionMapper.addMateRequistionModel(mateRequistionModel);
            }
            num = mateRequistionMapper.updateMateRequistion(mateRequistion);
        }

        return num;
    }

    @Override
    public int addManageInput(ManageInput manageInput) {
        int a = mateRequistionMapper.addManageInput(manageInput);
        if (manageInput.getInputModels().size() != 0) {
            for (ManageInputModel manageInputModel : manageInput.getInputModels()) {
                manageInputModel.setInputId(manageInput.getInputId());
                mateRequistionMapper.addManageInputModel(manageInputModel);
            }
        }
        return a;
    }

    @Override
    public int addProductRetention(ProductRetention productRetention) {
        int a = mateRequistionMapper.addProductRetention(productRetention);
        if (productRetention.getProductRetentionModels().size() != 0) {
            for (ProductRetentionModel productRetentionModel : productRetention.getProductRetentionModels()) {
                productRetentionModel.setRetentionId(productRetention.getRetentionId());
                mateRequistionMapper.addProductRetentionModel(productRetentionModel);
            }
        }
        return a;
    }

    @Override
    public int addSampleRequisition(SampleRequistion sampleRequistion) {
        int num = mateRequistionMapper.addSampleRequisition(sampleRequistion);
        if (sampleRequistion.getRequistionModels().size() != 0) {
            for (SampleRequistionModel sampleRequistionModel : sampleRequistion.getRequistionModels()) {
                sampleRequistionModel.setSampleId(sampleRequistion.getSampleId());
                mateRequistionMapper.addSampleRequisitionModel(sampleRequistionModel);
            }
        }
        return num;
    }

    @Override
    public int addKeepSample(KeepSample keepSample) {
        int num = mateRequistionMapper.addKeepSample(keepSample);
        if (keepSample.getKeepSampleModels().size() != 0) {
            for (KeepSampleModel keepSampleModel : keepSample.getKeepSampleModels()) {
                keepSampleModel.setKeepId(keepSample.getKeepId());
                mateRequistionMapper.addKeepSampleModel(keepSampleModel);
            }
        }
        return num;
    }

    @Override
    public int addSampleHandle(SampleHandle sampleHandle) {
        int num = mateRequistionMapper.addSampleHandle(sampleHandle);
        if (sampleHandle.getHandleModels().size() != 0) {
            for (SampleHandleModel sampleHandleModel : sampleHandle.getHandleModels()) {
                sampleHandleModel.setSampleHandleId(sampleHandle.getSampleHandleId());
                mateRequistionMapper.addSampleHandleModel(sampleHandleModel);
            }
        }
        return num;
    }

    @Override
    public int addManageSign(ManageSign manageSign) {
        int num = mateRequistionMapper.addManageSign(manageSign);
        if (manageSign.getManageSignModels().size() != 0) {
            for (ManageSignModel manageSignModel : manageSign.getManageSignModels()) {
                manageSignModel.setManageSignId(manageSign.getManageSignId());
                mateRequistionMapper.addManageSignModel(manageSignModel);
            }
        }
        return num;
    }

}
